import { getUsersApi, postUsersApi } from '@/api/User/Users'

const state = {
  queryInfo: {
    // 搜索关键字 和搜索框的value进行了双向绑定
    query: '',
    // 当前的页数
    pagenum: 1,
    // 当前每页显示多少条数据
    pagesize: 3
  },
  userlist: [],
  total: 0
}

// getters
const getters = {

}

// mutations
const mutations = {
  PAGE (state, num) {
    state.queryInfo.pagenum = num
  },
  PAGESIZE (state, num) {
    state.queryInfo.pagesize = num
  },
  QUERY (state, query) {
    state.queryInfo.query = query
  },
  USERLIST_TOTAL (state, { data: { users, total } }) {
    state.userlist = users
    state.total = total
  }
}

// actions
const actions = {
  async getUsers ({ state, commit }) {
    const { data } = await getUsersApi(state.queryInfo)
    if (data.meta.status !== 200) {
      return false
    }
    commit('USERLIST_TOTAL', data)
    return true
  },
  async postUsers (_, addFrom) {
    const res = await postUsersApi(addFrom)
    return res.data
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
